package com.atwl.school_system.controller;

import com.atwl.school_system.bean.CartInfo;
import com.atwl.school_system.mapping.CartDao;
import com.atwl.school_system.service.CartService;
import com.atwl.school_system.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
public class CartController {

    @Autowired
    private CartService cartService;

    @Autowired
    private CartDao cartDao;

    /**
     * 加入购物车
     */
    @GetMapping("/cart/add")
    public R addCart(@RequestParam("userId") Integer userId,
                     @RequestParam("proId") Integer proId,
                     @RequestParam("bookingTime") String bookingTime,
                     @RequestParam("proNum") Integer proNum) {
        cartService.addCart(userId, proId, proNum, bookingTime);
        return R.ok();
    }

    /**
     * 获取购物车商品
     */
    @GetMapping("/cart/get")
    public R getCart(@RequestParam("userId") Integer userId) {
        List<CartInfo> carts = cartDao.getCarts(userId);
        return R.ok().setData(carts);
    }

    @GetMapping("/cart/getByIds")
    public R getCartByIds(@RequestParam("ids") String cartIds) {
        System.out.println(cartIds);
        String[] cartIds_s = cartIds.split(",");
        List<CartInfo> cartInfoList = new ArrayList<>();
        for (String carId : cartIds_s) {
            int id = Integer.parseInt(carId);
            CartInfo cartInfo = cartDao.getId(id);
            cartInfoList.add(cartInfo);
        }
        return R.ok().setData(cartInfoList);
    }

    /**
     * 修改
     */
    @RequestMapping(path = "/cart/update", method = RequestMethod.POST)
    public R updateCart(@RequestBody CartInfo cartInfo) {

        cartService.updateCart(cartInfo);
        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/cart/delete")
    public R deleteCart(@RequestParam("id") Integer id) {
        cartService.deleteCart(id);
        return R.ok();
    }
}
